

const path = require('path');
const staticImg = path.join(__dirname, 'static');

function SensorbitUnoBlocks(Blockly) {
    var base_color = "#179b73";





      //旋转编码器初始化
      Blockly.Blocks.microbit_encoderInit = {
        init:function(){
        this.setColour(base_color);
        this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(staticImg+"/15.Rotary  encoder Module.svg", 84, 42))
            .appendField(Blockly.Msg.MICROBIT_ENCODERINIT)
            .appendField(new Blockly.FieldDropdown([
                ['P0', "pin0"],
                ['P13', "pin13"],
            ]),"aPort")
            this.appendDummyInput("")
            .appendField(Blockly.Msg.MICROBIT_ENCODERBPORTINIT)
            .appendField(new Blockly.FieldDropdown([
                ['P1', "pin1"],
                ['P2', "pin2"],
            ]),"bPort")
            this.appendDummyInput("")
            .appendField(Blockly.Msg.MICROBIT_ENCODERDPORTINIT)
            .appendField(new Blockly.FieldDropdown([
                ['P8', "pin8"],
                ['P15', "pin15"],
            ]),"dPort")
            this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
            this.setPreviousStatement(true, null);
           this.setNextStatement(true, null);
           this.setTooltip('');
        }
    };

    //旋转编码器按钮是否被按下
    Blockly.Blocks.microbit_encoderButtonPressed = {
        init:function(){
        this.setColour(base_color);
        this.appendDummyInput("")
        .appendField(new Blockly.FieldImage(staticImg+"/15.Rotary  encoder Module.svg", 84, 42))
        .appendField(Blockly.Msg.MICROBIT_ENCODERBUTTON)
        this.appendDummyInput("")
        .appendField(Blockly.Msg.MICROBIT_ENCODERDPORTINIT)
        .appendField(new Blockly.FieldDropdown([
            ['P8', "pin8"],
            ['P15', "pin15"],
        ]),"dPort")
        this.appendDummyInput("")
        .appendField(Blockly.Msg.MICROBIT_ENCODERBUTTONISPRESSED)
        
        
        this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL);
            this.setOutput(true, 'Boolean');
            this.setTooltip('');
        }
    };

     //旋转编码器按获取编码值
     Blockly.Blocks.microbit_encoderPulse = {
        init:function(){
            this.setColour(base_color);
            this.appendDummyInput("")
            .appendField(new Blockly.FieldImage(staticImg+"/15.Rotary  encoder Module.svg", 84, 42))
            .appendField(Blockly.Msg.MICROBIT_READENCODE)
            .appendField(Blockly.Msg.MICROBIT_PINA)
            .appendField(new Blockly.FieldDropdown([
                ['P0', "pin0"],
                ['P13', "pin13"],
            ]),"aPort")
            this.appendDummyInput("")
            .appendField(Blockly.Msg.MICROBIT_PINB)
            .appendField(new Blockly.FieldDropdown([
                ['P1', "pin1"],
                ['P2', "pin2"],
            ]),"bPort")
            .appendField(Blockly.Msg.MICROBIT_ENCODERPULSE)
            this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
            this.setOutput(true, 'Number');
            this.setTooltip('');
        }
    };

    return Blockly;
}

module.exports = SensorbitUnoBlocks;

